package chart;

import java.util.function.Consumer;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Side;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.chart.PieChart;
import javafx.scene.chart.PieChart.Data;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

// 饼状图
public class PieChartDemo extends Application {
	private Pane root = new Pane();
	private PieChart pc = new PieChart();
	private ObservableList<PieChart.Data> datas = FXCollections.observableArrayList();
	
	public static void main(String[] args) {
		launch(args);
	}
	
	@Override
	public void start(Stage stage) throws Exception {
		
		PieChart.Data data1 = new PieChart.Data("A", 20);	// 数量随意,最后按比例显示
		PieChart.Data data2 = new PieChart.Data("B", 30);
		PieChart.Data data3 = new PieChart.Data("C", 20);
		PieChart.Data data4 = new PieChart.Data("D", 40);
		PieChart.Data data5 = new PieChart.Data("E", 20);
		datas.add(data1);
		datas.add(data2);
		datas.add(data3);
		datas.add(data4);
		datas.add(data5);
		
		pc.setData(datas);
		pc.setLabelLineLength(20);	// 设置标签线的长度
		pc.setLabelsVisible(true);	// 标签的可见性
		pc.setLegendVisible(true);	// 说明的可见性
//		pc.setLegendSide(Side.LEFT);
		pc.setTitle("数据图");
		pc.setTitleSide(Side.TOP);
		pc.setAnimated(true); 	// 显示动画效果
		pc.setStyle("-fx-background-color: pink");
		pc.setPrefSize(300, 300);
		pc.getData().forEach(new Consumer<PieChart.Data>() {	// 设置提示条

			@Override
			public void accept(Data t) {
				Node node = t.getNode();
				Tooltip tooltip = new Tooltip(t.getName() + "-" + t.getPieValue());
				Tooltip.install(node, tooltip);
				
				node.setOnMouseClicked(e -> System.out.println(t.getName()) );
			}
		});
		root.getChildren().add(pc);
		
		Scene scene = new Scene(root, 400, 400);
		stage.setScene(scene);
		stage.show();
	}
}
